<template>
  <div>
    <el-dialog :title="info.isadd?'菜单添加':'菜单修改'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="菜单名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px">
          <el-select v-model="form.pid" placeholder="请选择上级菜单" @change="dingji">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" label-width="120px">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled>菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单图标" label-width="120px" v-if="this.form.type==1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option v-for="item in icons" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" label-width="120px" v-else>
          <el-select v-model="form.url" placeholder="请选择菜地址">
            <el-option
              v-for="item in IndexRoutes"
              :key="item.path"
              :value="'/'+item.path"
            >{{item.meta.title}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qx">取 消</el-button>
        <el-button type="primary" v-if="info.isadd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { IndexRoutes } from "../../../router";
import { reqmenuadd, reqmenuinfo, reqmenuedit } from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
export default {
  props: ["info", "list"],
  data() {
    return {
      IndexRoutes,
      //图标
      icons: [
        "el-icon-user-solid",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-s-help",
        "el-icon-camera-solid",
        "el-icon-s-platform",
        "el-icon-trophy",
      ],
      form: {
        title: "",
        icon: "",
        type: "",
        url: "",
        status: "", //1正常2禁用   number类型
      },
    };
  },
  methods: {
    qx() {
      this.info.isshow = false;
    },
    closed() {
      if (!this.info.isadd) {
        this.empty();
      }
    },
    empty() {
      this.form = {
        pid: "",
        title: "",
        icon: "",
        type: "",
        url: "",
        status: 2,
      };
    },
    checkEmpty() {
      return new Promise((resolve, reject) => {
        if (this.form.title == "") {
          errorAlert("菜单名称不能为空");
          return;
        }
        if (this.form.type == "") {
          errorAlert("上级菜单不能为空");
          return;
        }
        if (this.form.url == "") {
          errorAlert("菜单不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
        reqmenuadd(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);

            this.qx();

            this.empty();

            this.$emit("init");
          }
        });
    },
    //请求一条数据
    one(id) {
      reqmenuinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;
        }
      });
    },
    //修改
    update() {
        reqmenuedit(this.form).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);
            this.qx();
            this.empty();
            this.$emit("init");
            // console.log(this.form);
          }
        });
    },
    //判断是不是顶级菜单
    dingji() {
      if (this.form.pid === 0) {
        this.form.type = 1;
      } else {
        this.form.type = 2;
      }
    },
  },
};
</script>

<style>
</style>